require.config({
	shim:{
        'calendar':{exports:'calendar'}
    },
    paths : {
        //"vue":"lib/vue",
        "jquery":"lib/jquery",
        "cmd":"cmd",
        "calendar":"lib/datepicker/WdatePicker",
    }
});

require(['jquery','cmd','calendar'],function($){

//get top menu
var token = detectToken();


//页面加载，载入事件列表
$.vloading.show();
$.ajax({
        type:'POST',
        url:urls.eventLists,
        dataType:'json',
        data:{token:token,pageNow:1,pageSize:"20"},
        async:true,
        success:function(result){
           $.vloading.remove();
            if(result.code=="1"){
                if(result.data.records.length==0){
                    $(".showtableContent").html('<p class="txtc" style="padding:19px 0;font-size:16px;">暂无数据</p>');
                }else{
                    var tableTbodyHtml = '';
                    for(var i=0;i<result.data.records.length;i++){
                        if(result.data.records[i].is_read=="0"){//未读
                            tableTbodyHtml += '<div data-eventid="'+result.data.records[i].event_id+'" data-id="'+result.data.records[i].id+'" class="tableTReach clearfix"><div class="tablecommoncel-1 tableTBdpoint fl"><i class="xwl i-checkno"></i></div><div class="tablecommoncel-2 tableTBdBH fl oellipsis">'+result.data.records[i].dtu_id+'</div><div class="tablecommoncel-3 tableTBdType fl">'+result.data.records[i].type_name+'</div><div class="tablecommoncel-4 tableTBdMsg fl oellipsis">'+result.data.records[i].event_name+'</div><div class="tablecommoncel-5 tableTBdpSbBH fl">'+result.data.records[i].device_no+'</div><div class="tablecommoncel-6 tableTBdnName fl oellipsis">'+result.data.records[i].customer_name+'</div><div class="tablecommoncel-7 tableTBdpStatus fl">未处理<i class="xwl i-nodel pdl5 red"></i></div><div class="tablecommoncel-8 tableTBdpTime fl">'+result.data.records[i].create_date+'</div></div>';
                        }else{
                            tableTbodyHtml += '<div data-eventid="'+result.data.records[i].event_id+'" data-id="'+result.data.records[i].id+'" class="tableTReach clearfix"><div class="tablecommoncel-1 tableTBdpoint fl"><i class="xwl i-checkno"></i></div><div class="tablecommoncel-2 tableTBdBH fl oellipsis">'+result.data.records[i].dtu_id+'</div><div class="tablecommoncel-3 tableTBdType fl">'+result.data.records[i].type_name+'</div><div class="tablecommoncel-4 tableTBdMsg fl oellipsis">'+result.data.records[i].event_name+'</div><div class="tablecommoncel-5 tableTBdpSbBH fl">'+result.data.records[i].device_no+'</div><div class="tablecommoncel-6 tableTBdnName fl oellipsis">'+result.data.records[i].customer_name+'</div><div class="tablecommoncel-7 tableTBdpStatus fl">已处理<i class="xwl i-dealed pdl5 green"></i></div><div class="tablecommoncel-8 tableTBdpTime fl">'+result.data.records[i].create_date+'</div></div>';
                        }
                    }
                    $(".showtableContent").html(tableTbodyHtml);            
                }
                //总页数
                $(".allpagesNums").html(result.data.pageCount);
                //总记录数
                $(".allRecord").html(result.data.rowCount);
                pageNav.go(1,result.data.pageCount);
                pageNav.fn = function(p,pn){
                        if($("#timerfilterBtn").attr("data-coding")==0){
                            loadEventlist(p);
                            return;
                        }
                };
                //单选状态
                $(".tableTBdpoint").toggle(
                  function(){$(this).find("i").addClass("i-checked").removeClass("i-checkno");$(this).addClass("i-xuanzele").removeClass("i-meixuanze").parent(".tableTReach").css("background","#F4F4F4");},
                  function(){$(this).find("i").addClass("i-checkno").removeClass("i-checked");$(this).removeClass("i-xuanzele").addClass("i-meixuanze").parent(".tableTReach").css("background","#fff");}
                );

            }
        }
});
var loadEventlist = function(curNums){
    $.vloading.show();
    $.ajax({
            type:'POST',
            url:urls.eventLists,
            dataType:'json',
            data:{token:token,pageNow:curNums,pageSize:"20"},
            async:true,
            success:function(result){
               $.vloading.remove();
                if(result.code=="1"){
                    if(result.data.records.length==0){
                        $(".showtableContent").html('<p class="txtc" style="padding:19px 0;font-size:16px;">暂无数据</p>');
                    }else{
                        var tableTbodyHtml = '';
                        for(var i=0;i<result.data.records.length;i++){
                            if(result.data.records[i].is_read=="0"){//未读
                                tableTbodyHtml += '<div data-eventid="'+result.data.records[i].event_id+'" data-id="'+result.data.records[i].id+'" class="tableTReach clearfix"><div class="tablecommoncel-1 tableTBdpoint fl"><i class="xwl i-checkno"></i></div><div class="tablecommoncel-2 tableTBdBH fl oellipsis">'+result.data.records[i].dtu_id+'</div><div class="tablecommoncel-3 tableTBdType fl">'+result.data.records[i].type_name+'</div><div class="tablecommoncel-4 tableTBdMsg fl oellipsis">'+result.data.records[i].event_name+'</div><div class="tablecommoncel-5 tableTBdpSbBH fl">'+result.data.records[i].device_no+'</div><div class="tablecommoncel-6 tableTBdnName fl oellipsis">'+result.data.records[i].customer_name+'</div><div class="tablecommoncel-7 tableTBdpStatus fl">未处理<i class="xwl i-nodel pdl5 red"></i></div><div class="tablecommoncel-8 tableTBdpTime fl">'+result.data.records[i].create_date+'</div></div>';
                            }else{
                                tableTbodyHtml += '<div data-eventid="'+result.data.records[i].event_id+'" data-id="'+result.data.records[i].id+'" class="tableTReach clearfix"><div class="tablecommoncel-1 tableTBdpoint fl"><i class="xwl i-checkno"></i></div><div class="tablecommoncel-2 tableTBdBH fl oellipsis">'+result.data.records[i].dtu_id+'</div><div class="tablecommoncel-3 tableTBdType fl">'+result.data.records[i].type_name+'</div><div class="tablecommoncel-4 tableTBdMsg fl oellipsis">'+result.data.records[i].event_name+'</div><div class="tablecommoncel-5 tableTBdpSbBH fl">'+result.data.records[i].device_no+'</div><div class="tablecommoncel-6 tableTBdnName fl oellipsis">'+result.data.records[i].customer_name+'</div><div class="tablecommoncel-7 tableTBdpStatus fl">已处理<i class="xwl i-dealed pdl5 green"></i></div><div class="tablecommoncel-8 tableTBdpTime fl">'+result.data.records[i].create_date+'</div></div>';
                            }
                        }
                        $(".showtableContent").html(tableTbodyHtml);            
                    }
                    //总页数
                    $(".allpagesNums").html(result.data.pageCount);
                    //总记录数
                    $(".allRecord").html(result.data.rowCount);
                    //单选状态
                    $(".tableTBdpoint").toggle(
                      function(){$(this).find("i").addClass("i-checked").removeClass("i-checkno");$(this).addClass("i-xuanzele").removeClass("i-meixuanze").parent(".tableTReach").css("background","#F4F4F4");},
                      function(){$(this).find("i").addClass("i-checkno").removeClass("i-checked");$(this).removeClass("i-xuanzele").addClass("i-meixuanze").parent(".tableTReach").css("background","#fff");}
                    );

                }
            }
    });    
}



//事件类别 
$.post(urls.eventTypes,{token:token},function(result){
    if(result.code=="1"){
        var eveTypesHtml = '';
        for(var i=0;i<result.data.length;i++){
            eveTypesHtml += '<option value="'+result.data[i].id+'">'+result.data[i].type_name+'</option>';
        }
        $("#eveTypes").html('<option value="" selected="selected">事件类别</option>'+eveTypesHtml);
    }
},"json");     
//根据类别获取事件类型
$("#eveTypes").change(function(){ 
    var event_type = $(this).val();
    $.post(urls.eventTypesName,{token:token,event_type:event_type},function(result){
        if(result.code=="1"){
            var evetypesNameHtml = '';
            for(var i=0;i<result.data.length;i++){
                evetypesNameHtml += '<option value="'+result.data[i].id+'">'+result.data[i].event_name+'</option>';
            }
            $("#evetypesName").html('<option value="" selected="selected">事件类型</option>'+evetypesNameHtml);
        }
    },"json");
});


//全选状态
$(".tableTHpoint").toggle(
    function(){$(".tableTHpoint,.tableTBdpoint").find("i").addClass("i-checked").removeClass("i-checkno");$(".tableTBdpoint").addClass("i-xuanzele").removeClass("i-meixuanze").parent(".tableTReach").css("background","#F4F4F4");},
    function(){$(".tableTHpoint,.tableTBdpoint").find("i").removeClass("i-checked").addClass("i-checkno");$(".tableTBdpoint").removeClass("i-xuanzele").addClass("i-meixuanze").parent(".tableTReach").css("background","#fff");}
)

getOnelayerMenu();
getTwolayerMenu(301,302);
$(".mianshowbpt-r a").eq(0).addClass("cur");
getThreelayerMenu(302);


//点击删除按钮，更新获取所有被选中的checked的事件id,过滤没选中任何事件情况
$("#eventsDel").click(function(){
    var checkedEventsNums = $(".i-xuanzele");
    var checkedEventsNums_2 = $(".tableTbody .i-checked");
    var ids=[];
    if(checkedEventsNums_2.length==0){
        $.flytip("请选择事件");
    }else{
        for(var i=0;i<checkedEventsNums.length;i++){
            ids.push($(".i-xuanzele").eq(i).parent(".tableTReach").attr("data-id"));
        }
        $(".questionLayer,.masking").show();
        //点击确认按钮
        $(".questionlayer-Sure").on("click",function(){
            //删除事件（可批量）
            $.post(urls.deleteEvents,{token:token,ids:ids.join(",")},function(result){
                if(result.code=="1"){
                    $.flytip("操作成功！");
                    $(".i-xuanzele").parent(".tableTReach").remove();
                    var visible_nums = $(".tableTReach:visible").length; 
                    if(visible_nums==0){
                        window.location.reload();
                    }
                    
                }
            },"json");
            $(".questionLayer,.masking").hide();
                       
        })
        $(".closeLayerbox").on("click",function(){
            $(".questionLayer,.masking").hide();
        })

        
    }   
})
//导出
$("#daochuImg").on("click",function(){
    var event_type = $("#eveTypes").val();
    var dtu_id = $("#eventDtuId").val();
    var customer_name = $("#eventkehuName").val();
    var is_read = $("#eventIsread").val();
    var createStartDate = $("#startDate").val();
    var createStartEnd = $("#endDate").val();
    var event_id = $("#evetypesName").val();
    var id = [];
    var checkedEventsNums = document.querySelectorAll(".i-xuanzele");
    for(var i=0;i<checkedEventsNums.length;i++){
        id.push($(".i-xuanzele").eq(i).parent(".tableTReach").attr("data-id"));
    }
    /*$.post(urls.getFastView,{token:token,event_type:event_type,dtu_id:dtu_id,customer_name:customer_name,is_read:is_read,event_id:event_id,id:id.join(","),createStartDate:createStartDate,createStartEnd:createStartEnd},function(result){
        if(result.code=="1"){
            $.flytip("导出成功！");
            
        }
    },"json");*/
    //导出START
    var downloadURL =  urls.getFastView+"?token="+token+"&event_type="+event_type+"&dtu_id="+dtu_id+"&customer_name="+customer_name+"&is_read="+is_read+"&event_id="+event_id+"&id="+id.join(",")+"&createStartDate="+createStartDate+"&createStartEnd="+createStartEnd;
    var iframe = document.createElement("iframe");  
    iframe.src = downloadURL;  
    iframe.style.display = "none";  
    document.body.appendChild(iframe); 
    //导出END
})

//点击处理事件按钮
$("#eventsHandel").click(function(){
    var checkedEventsNums = document.querySelectorAll(".i-xuanzele");
    var checkedEventsNums_2 = document.querySelectorAll(".tableTbody .i-checked");
    var ids=[];
    if(checkedEventsNums_2.length==0){
        $.flytip("请选择事件");
    }else{
        for(var i=0;i<checkedEventsNums.length;i++){
            //ids.push(checkedEventsNums[i].parentNode.attributes[0].value);
            ids.push($(".i-xuanzele").eq(i).parent(".tableTReach").attr("data-id"));
        }
        $(".questionLayer2,.masking").show();
        //点击确认按钮
        $(".questionlayer-Sure2").on("click",function(){
            //处理事件（可批量）
            $.post(urls.handleEvents,{token:token,ids:ids.join(","),is_read:1},function(result){
                if(result.code=="1"){
                    $.flytip("操作成功！");
                    $(".i-xuanzele").parent(".tableTReach").find(".tableTBdpStatus").html('已处理<i class="xwl i-dealed pdl5 green"></i>');
                }
            },"json");
            $(".questionLayer2,.masking").hide();
                       
        })
        $(".closeLayerbox").on("click",function(){
            $(".questionLayer2,.masking").hide();
        })

        
    }   
})
//点击查询事件
$("#timerfilterBtn").on("click",function(){
    $("#timerfilterBtn").attr("data-coding","1");
    var dtu_id = $("#eventDtuId").val();
    var customer_name = $("#eventkehuName").val();
    var event_type = $("#eveTypes").val();
    var event_id = $("#evetypesName").val();
    var is_read = $("#eventIsread").val();
    var createStartDate = $("#startDate").val();
    var createStartEnd = $("#endDate").val();
    var pageNow = 1;
    var pageSize = 20;
    $.vloading.show();
    $.post(urls.eventLists,{token:token,dtu_id:dtu_id,customer_name:customer_name,event_type:event_type,event_id:event_id,is_read:is_read,createStartDate:createStartDate,createStartEnd:createStartEnd,pageNow:pageNow,pageSize:pageSize},function(result){
        $.vloading.remove();
        if(result.code=="1"){
            if(result.data.records.length==0){
                $(".showtableContent").html('<p class="txtc" style="padding:19px 0;font-size:16px;border-right:1px solid #e9e9e9;border-bottom:1px solid #e9e9e9;">暂无数据</p>');
            }else{
                var tableTbodyHtml = '';
                for(var i=0;i<result.data.records.length;i++){
                    if(result.data.records[i].is_read=="0"){//未读
                        tableTbodyHtml += '<div data-eventid="'+result.data.records[i].event_id+'" data-id="'+result.data.records[i].id+'" class="tableTReach clearfix"><div class="tablecommoncel-1 tableTBdpoint fl"><i class="xwl i-checkno"></i></div><div class="tablecommoncel-2 tableTBdBH fl oellipsis">'+result.data.records[i].dtu_id+'</div><div class="tablecommoncel-3 tableTBdType fl">'+result.data.records[i].type_name+'</div><div class="tablecommoncel-4 tableTBdMsg fl oellipsis">'+result.data.records[i].event_name+'</div><div class="tablecommoncel-5 tableTBdpSbBH fl">'+result.data.records[i].device_no+'</div><div class="tablecommoncel-6 tableTBdnName fl oellipsis">'+result.data.records[i].customer_name+'</div><div class="tablecommoncel-7 tableTBdpStatus fl">未处理<i class="xwl i-nodel pdl5 red"></i></div><div class="tablecommoncel-8 tableTBdpTime fl">'+result.data.records[i].create_date+'</div></div>';
                    }else{
                        tableTbodyHtml += '<div data-eventid="'+result.data.records[i].event_id+'" data-id="'+result.data.records[i].id+'" class="tableTReach clearfix"><div class="tablecommoncel-1 tableTBdpoint fl"><i class="xwl i-checkno"></i></div><div class="tablecommoncel-2 tableTBdBH fl oellipsis">'+result.data.records[i].dtu_id+'</div><div class="tablecommoncel-3 tableTBdType fl">'+result.data.records[i].type_name+'</div><div class="tablecommoncel-4 tableTBdMsg fl oellipsis">'+result.data.records[i].event_name+'</div><div class="tablecommoncel-5 tableTBdpSbBH fl">'+result.data.records[i].device_no+'</div><div class="tablecommoncel-6 tableTBdnName fl oellipsis">'+result.data.records[i].customer_name+'</div><div class="tablecommoncel-7 tableTBdpStatus fl">已处理<i class="xwl i-dealed pdl5 green"></i></div><div class="tablecommoncel-8 tableTBdpTime fl">'+result.data.records[i].create_date+'</div></div>';
                    }
                }
                $(".showtableContent").html(tableTbodyHtml);                
            }
            //总页数
            $(".allpagesNums").html(result.data.pageCount);
            //总记录数
            $(".allRecord").html(result.data.rowCount);
            pageNav.go(1,result.data.pageCount);
            pageNav.fn = function(p,pn){
                getSearchEventlist(p);
            };
            //单选状态
            $(".tableTBdpoint").toggle(
              function(){$(this).find("i").addClass("i-checked").removeClass("i-checkno");$(this).addClass("i-xuanzele").removeClass("i-meixuanze").parent(".tableTReach").css("background","#F4F4F4");},
              function(){$(this).find("i").addClass("i-checkno").removeClass("i-checked");$(this).removeClass("i-xuanzele").addClass("i-meixuanze").parent(".tableTReach").css("background","#fff");}
            );

        }
    },"json");     

})

var getSearchEventlist = function(curNums){
    var dtu_id = $("#eventDtuId").val();
    var customer_name = $("#eventkehuName").val();
    var event_type = $("#eveTypes").val();
    var event_id = $("#evetypesName").val();
    var is_read = $("#eventIsread").val();
    var createStartDate = $("#startDate").val();
    var createStartEnd = $("#endDate").val();
    var pageNow = curNums;
    var pageSize = 20;
    $.vloading.show();
    $.post(urls.eventLists,{token:token,dtu_id:dtu_id,customer_name:customer_name,event_type:event_type,event_id:event_id,is_read:is_read,createStartDate:createStartDate,createStartEnd:createStartEnd,pageNow:curNums,pageSize:pageSize},function(result){
        $.vloading.remove();
        if(result.code=="1"){
            if(result.data.records.length==0){
                $(".showtableContent").html('<p class="txtc" style="padding:19px 0;font-size:16px;border-right:1px solid #e9e9e9;border-bottom:1px solid #e9e9e9;">暂无数据</p>');
            }else{
                var tableTbodyHtml = '';
                for(var i=0;i<result.data.records.length;i++){
                    if(result.data.records[i].is_read=="0"){//未读
                        tableTbodyHtml += '<div data-eventid="'+result.data.records[i].event_id+'" data-id="'+result.data.records[i].id+'" class="tableTReach clearfix"><div class="tablecommoncel-1 tableTBdpoint fl"><i class="xwl i-checkno"></i></div><div class="tablecommoncel-2 tableTBdBH fl oellipsis">'+result.data.records[i].dtu_id+'</div><div class="tablecommoncel-3 tableTBdType fl">'+result.data.records[i].type_name+'</div><div class="tablecommoncel-4 tableTBdMsg fl oellipsis">'+result.data.records[i].event_name+'</div><div class="tablecommoncel-5 tableTBdpSbBH fl">'+result.data.records[i].device_no+'</div><div class="tablecommoncel-6 tableTBdnName fl oellipsis">'+result.data.records[i].customer_name+'</div><div class="tablecommoncel-7 tableTBdpStatus fl">未处理<i class="xwl i-nodel pdl5 red"></i></div><div class="tablecommoncel-8 tableTBdpTime fl">'+result.data.records[i].create_date+'</div></div>';
                    }else{
                        tableTbodyHtml += '<div data-eventid="'+result.data.records[i].event_id+'" data-id="'+result.data.records[i].id+'" class="tableTReach clearfix"><div class="tablecommoncel-1 tableTBdpoint fl"><i class="xwl i-checkno"></i></div><div class="tablecommoncel-2 tableTBdBH fl oellipsis">'+result.data.records[i].dtu_id+'</div><div class="tablecommoncel-3 tableTBdType fl">'+result.data.records[i].type_name+'</div><div class="tablecommoncel-4 tableTBdMsg fl oellipsis">'+result.data.records[i].event_name+'</div><div class="tablecommoncel-5 tableTBdpSbBH fl">'+result.data.records[i].device_no+'</div><div class="tablecommoncel-6 tableTBdnName fl oellipsis">'+result.data.records[i].customer_name+'</div><div class="tablecommoncel-7 tableTBdpStatus fl">已处理<i class="xwl i-dealed pdl5 green"></i></div><div class="tablecommoncel-8 tableTBdpTime fl">'+result.data.records[i].create_date+'</div></div>';
                    }
                }
                $(".showtableContent").html(tableTbodyHtml);                
            }
            //总页数
            $(".allpagesNums").html(result.data.pageCount);
            //总记录数
            $(".allRecord").html(result.data.rowCount);
            //单选状态
            $(".tableTBdpoint").toggle(
              function(){$(this).find("i").addClass("i-checked").removeClass("i-checkno");$(this).addClass("i-xuanzele").removeClass("i-meixuanze").parent(".tableTReach").css("background","#F4F4F4");},
              function(){$(this).find("i").addClass("i-checkno").removeClass("i-checked");$(this).removeClass("i-xuanzele").addClass("i-meixuanze").parent(".tableTReach").css("background","#fff");}
            );

        }
    },"json");     

    
}

//分页器optional set
//事件监听分页触发时，获得当前页码数
pageNav.pre="上一页";
pageNav.next="下一页";








//个人中心下拉
$("#showmyAccountMenu").toggle(
    function(){$("#toggleshowmenu").show();},
    function(){$("#toggleshowmenu").hide();}
);

//$.flytip("zhowdom");$.vloading.show();$.vloading.remove();




//req.js end
})